/*
 * GroupBean.java
 *
 * Created on August 17, 2007, 5:12 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package timeitj.ejb.groups;

import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import org.apache.log4j.Logger;
import timeitj.ejb.persistence.PersistenceManagerAbstract;
import timeitj.persistence.Group;

/**
 *
 * @author wallner
 */

@Stateless
public class GroupManagerBean extends PersistenceManagerAbstract<Group, Long> implements GroupManager {
    
    public GroupManagerBean(){
        log = Logger.getLogger(GroupManagerBean.class.getName());
        log.debug("GroupManagerBean instantiated");
    }
    
    /**
     *
     * Returns the Group with the given name.
     * Returns the Group with the name given as argument or null if no such group exists
     */
    public Group getGroupByName(String name){
        Group group;
        try {
            group = (Group) em.createNamedQuery("GroupByName")
            .setParameter("name", name)
            .getSingleResult();
        } catch (NoResultException ex) {
            return null;
        }
        return group;
    }
    
    /**
     * create a Group of a given name
     */
    public Group createGroup(String name){
        Group group = new Group(name);
        store(group);
        log.info("New Group created: " + group.getGroupName());
        return group;
    }
}
